/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.engine;

import axescon.xacml.api.CtxHandler;
import axescon.xacml.model.ctx.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Date: Sep 27, 2005
 *         Responsibilities:
 */
public class BaseCtxHandler implements CtxHandler {

/*
    public AttributeValue getResourceAttribute(XacmlCtx req, String id) {
        return null;
    }
*/
    /**
     * this assumes only one resource
     *
     * @param req
     * @return
     */
    public String getResourceId(XacmlCtx req) {
        Resource res = req.getCurrentResource();
        return res.getResourceId();
    }

    /**
     * @param req
     * @return List<Attribute>
     */
    public Attribute[] getEnvironmentAttributeArray(XacmlCtx req) {
        return req.getEnvironment().getAttributeArray();
    }


    public Attribute[] getActionAttributeArray(XacmlCtx req) {
        return req.getAction().getAttributeArray();
    }

    /**
     * @param req
     * @return
     */
    public Attribute[] getResourceAttributeArray(XacmlCtx req) {
        return req.getCurrentResource().getAttributeArray();
    }

    /**
     *
     * @param req
     * @return list with all attributes of given subject category
     */
/*
    public List<Attribute> getSubjectAttributeList(String category, XacmlCtx req) {
        List<Attribute> ret = new ArrayList();
        for(Subject s:req.getSubjectList()){
            if(!s.getSubjectCategory().equals(category))continue;
            ret.addAll(s.getAttributeList());
        }
        return ret;
    }
*/

    /**
     * @param req
     * @return list with all attributes of given subject category
     */
    public Attribute[] getSubjectAttributeArray(String attributeId, String category, XacmlCtx req) {
        List ret = new ArrayList();
        for (int i = 0; i < req.getSubjectArray().length; i++) {
            Subject s = req.getSubjectArray()[i];
            if (!s.getSubjectCategory().equals(category)) continue;
            for (int j = 0; j < s.getAttributeArray().length; j++) {
                Attribute a = s.getAttributeArray()[j];
                if (attributeId.equals(a.getAttributeId())) ret.add(a);
            }
        }
        return (Attribute[]) ret.toArray(new Attribute[ret.size()]);
    }
}
